<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2018/11/7
 * Time: 9:51
 */

namespace App\FreeBird\Repositories;

use App\FreeBird\Contracts\RebateLogInterface;
use App\FreeBird\Models\RebateLogModel;
use App\Weixin\Contracts\UserInterface;
use Illuminate\Support\Facades\Request;

class  DbRebateLogRepositories implements RebateLogInterface
{


    /**
     * 返佣列表模型
     * @return RebateLogModel
     */
    public function getRebateLogModel()
    {
        return app()->make(RebateLogModel::class);
    }

    /**
     * 用户接口
     * @return UserInterface
     */
    public function getUserInterface()
    {
        return app()->make(UserInterface::class);
    }

    /**
     * 保存返佣列表
     * @param $data
     */
    public function saveVipData($data)
    {

        //获取用户username
        $userInfo = $this->getUserInterface()->getUserInfoById(["uid" => $data['uid']]);

        if ($userInfo) {
            $data['username'] = $userInfo->truename;
        }
        $buserInfo = $this->getUserInterface()->getUserInfoById(["uid" => $data['buid']]);
        if ($buserInfo) {
            $data['busername'] = $buserInfo->truename;
        }

        if ($this->getRebateLogModel()->newQuery()->create($data)) {
            $remark = $data['type'] == 1 ? '购买vip-' : '求帮-';
            //$type=$data['source_type'] == 1 ? 4 : 5;
            if ($data['source_type'] == 1) {
                $type = 4;
                $remark .= '直推';
            } else if ($data['source_type'] == 2) {
                $type = 5;
                $remark .= '间推';
            } else if ($data['source_type'] == 3) {
                $type = 7;
                $remark .= '赏金';

            }

            //用户变动
            if ($data['mount_type'] == 1) {  //余额变动
                return $this->getUserInterface()->updateUserBalance($data['buid'], $data['b_mount'], $type, $data['order_sn'], $remark);
            } else if ($data['mount_type'] == 2) {         //金币变动
                return $this->getUserInterface()->updateUserScore($data['buid'], $data['b_mount'], $type, $data['order_sn'], $remark);
            }
        }
    }


    /**
     * 获取列表
     * @param $where
     * @return mixed
     */
    public function getList($where)
    {
        $request = Request::instance();
        $limit = empty($request->input("limit")) ? 10 : $request->input("limit");
        $sort = empty($request->input("sort")) ? 'create_time' : $request->input("sort");
        $order = empty($request->input("order")) ? 'desc' : $request->input("order");
        return $this->getRebateLogModel()->newQuery()->where($where)->orderBy($sort, $order)->paginate($limit);
    }


    public function getListHomeAll($where){
        $request = Request::instance();
        $limit = empty($request->input("limit")) ? 10 : $request->input("limit");
        $sort = empty($request->input("sort")) ? 'create_time' : $request->input("sort");
        $order = empty($request->input("order")) ? 'desc' : $request->input("order");
        return $this->getRebateLogModel()->newQuery()->with(["user"])->where($where)->orderBy($sort, $order)->paginate($limit);
    }
    /**
     * 获取统计列表
     */
    public function getStatistics()
    {
        //今天时间
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        //本月时间
        $beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
        $endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
        $allShouRu=$this->getRebateLogModel()->newQuery()->where("buid",AUID)->where("mount_type",1)->whereIn("source_type",[1,2])->sum("b_mount");
        $nowShouRu=$this->getRebateLogModel()->newQuery()->where("buid",AUID)->where("mount_type",1)->whereIn("source_type",[1,2])->whereBetween("create_time",[$beginToday,$endToday])->sum("b_mount");
        $monthShouRu=$this->getRebateLogModel()->newQuery()->where("buid",AUID)->where("mount_type",1)->whereIn("source_type",[1,2])->whereBetween("create_time",[$beginThismonth,$endThismonth])->sum("b_mount");
        $nowScoreShouRu=$this->getRebateLogModel()->newQuery()->where("buid",AUID)->where("mount_type",2)->whereIn("source_type",[1,2])->whereBetween("create_time",[$beginToday,$endToday])->sum("b_mount");
        //总条数
        $allZhiCount=$this->getRebateLogModel()->newQuery()->where("buid",AUID)->where("source_type",1)->count();
        $allJianCount=$this->getRebateLogModel()->newQuery()->where("buid",AUID)->where("source_type",2)->count();

        $returnArray=array();
        $returnArray["all_shou_ru"]=$allShouRu;  //总收入
        $returnArray["now_show_ru"]=$nowShouRu;   //日收入
        $returnArray["month_shou_ru"]=$monthShouRu;  //月收入
        $returnArray["now_score_shou_ru"]=$nowScoreShouRu; //金币总收入
        $returnArray["all_zhi_count"]=$allZhiCount; //直推总条数
        $returnArray["all_jian_count"]=$allJianCount; //间推总条数
        return $returnArray;
    }

    /**
     * 获取单条信息
     * @param $id
     * @return mixed
     */
    public function getInfo($id)
    {

    }
}